<template>
	<view class="manage-gameinfo">
		<uni-section title="管理游戏信息" type="line" class="playeraction-info">
			<view class="toggle-text">
				<uni-combox class="toggle-combox" :candidates="gameStageList" placeholder="游戏回合选择"
					v-model="selectedGameStage">
				</uni-combox>
<!-- 				{{gameStageActionList}}{{addActionList}} -->
<!-- 				{{selectedGameStageStatus}}
				{{selectedGameStage}} -->
			</view>
			<!-- 这里可以封装为异步组件，动态路由加载 减少渲染消耗-->
			<!-- 夜晚 狼人行为 -->
			<uni-card v-show="selectedGameStageStatus === 'night'" :is-shadow="true" class="action-card">
				<uni-title type="h2" title="狼人行为" color="#2490ff"></uni-title>
				<view class="action-text">
					狼人[{{werewolf.toString()}}]袭击了
					<view class="input">
						<uni-easyinput trim="all" v-model="inputWerewolfAttackNum" :styles="styles" placeholder="座位">
						</uni-easyinput>
					</view>
					号
				</view>
				<view class="action-text">
					狼美人[{{wolf_beauty}}]魅惑了
					<view class="input">
						<uni-easyinput trim="all" v-model="inputWolfBeautyCharmNum" :styles="styles" placeholder="座位">
						</uni-easyinput>
					</view>
					号 <text class="rest-tips">——剩余次数:{{charrest}}</text>
				</view>
			</uni-card>
			<!-- 夜晚 神职行为 -->
			<uni-card v-show="selectedGameStageStatus === 'night'" :is-shadow="true" class="action-card">
				<uni-title type="h2" title="神职行为" color="#2490ff"></uni-title>
				<view class="action-text">
					预言家[{{seer}}]查验了
					<view class="input">
						<uni-easyinput v-model="inputSeerCheckNum" trim="all" :styles="styles" placeholder="座位">
						</uni-easyinput>
					</view>
					号
				</view>
				<view class="action-text">
					女巫[{{witch}}]
					解药解救 {{inputWerewolfAttackNum}} 号
					<view class="input1">
						<uni-data-checkbox v-model="witchRescueRadio" :localdata="witchRescue"></uni-data-checkbox>
					</view>
					<text class="rest-tips">剩余次数:{{rescuerest}}</text>
				</view>
				<view class="action-text">
					女巫[{{witch}}]毒杀了
					<view class="input">
						<uni-easyinput trim="all" v-model="witchPoisonNum" :styles="styles" placeholder="座位">
						</uni-easyinput>
					</view>
					号 <text class="rest-tips">——剩余次数:{{poisonrest}}</text>
				</view>
				<view class="action-text">
					守卫[{{guarder}}]守护了
					<view class="input">
						<uni-easyinput trim="all" v-model="guarderGuardNum" :styles="styles" placeholder="座位">
						</uni-easyinput>
					</view>
					号
				</view>
			</uni-card>
			<!-- 白天 警长竞选行为 -->
			<uni-card v-show="selectedGameStageStatus === 'campaign'" :is-shadow="true" class="action-card">
				<uni-title type="h2" title="上警人员" color="#2490ff"></uni-title>
				<view class="action-text">
					<!-- v-for="(item,index) in game" 根据座位数渲染空位数-->
					<view class="input" v-for="(item,index) in currentGameDTO.setting.playerCount" :key="index">
						<uni-easyinput trim="all" v-model="campaignPoliceNums[index]" :styles="styles" placeholder="座位">
						</uni-easyinput>
					</view>
					号，上警
				</view>
			</uni-card>
			<!-- 白天 警长退水行为 -->
			<uni-card v-show="selectedGameStageStatus === 'campaign'" :is-shadow="true" class="action-card">
				<uni-title type="h2" title="退水人员" color="#2490ff"></uni-title>
				<view class="action-text">
					<view class="input" v-for="(item,index) in currentGameDTO.setting.playerCount" :key="index">
						<uni-easyinput trim="all" v-model="exitCampaignPoliceNums[index]" :styles="styles"
							placeholder="座位">
						</uni-easyinput>
					</view>
					号，退水
				</view>
			</uni-card>
			<!-- 白天 警长行为 -->
			<uni-card v-show="selectedGameStageStatus === 'day'" :is-shadow="true" class="action-card">
				<uni-title type="h2" title="警长行为" color="#2490ff"></uni-title>
				<!-- 发言顺序暂时不需要记录 -->
				<!-- <view class="action-text">
					<view class="input">
						<uni-easyinput trim="all" v-model="value" :styles="styles" 
							placeholder="座位" @input="campaignPoliceNum"></uni-easyinput>
					</view>号是本轮警长，选择警
					<view class="input1">
						<uni-data-checkbox v-model="talkTurnRadio" :localdata="talkTurn"></uni-data-checkbox>
					</view>发言
				</view> -->
				<view class="action-text">
					({{police}})号是本轮警长，选择归票
					<view class="input">
						<uni-easyinput trim="all" v-model="policeCallExileNum" :styles="styles" placeholder="座位">
						</uni-easyinput>
					</view>号
				</view>
				<view class="action-text">
					<!-- 					<view class="input">
						<uni-easyinput trim="all" v-model="value" :styles="styles" 
							placeholder="座位" @input="police"></uni-easyinput>
					</view> -->
					({{police}})号是本轮警长，将警徽移交给了<view class="input">
						<uni-easyinput trim="all" v-model="policeMoveNum" :styles="styles" placeholder="座位">
						</uni-easyinput>
					</view>号
				</view>
			</uni-card>
			<!-- 白天 狼人自爆行为 -->
			<uni-card v-show="selectedGameStageStatus === 'day'" :is-shadow="true" class="action-card">
				<uni-title type="h2" title="狼人自爆行为" color="#2490ff"></uni-title>
				<view class="action-text">
					<view class="input">
						<uni-easyinput trim="all" v-model="werewolfBoomNum" :styles="styles" placeholder="座位">
						</uni-easyinput>
					</view>号狼人，自爆
				</view>
				<view class="action-text">
					({{white_wolf_king}}) 号白狼王自爆，带走<view class="input">
						<uni-easyinput trim="all" v-model="whiteWolfKingShootNum" :styles="styles" placeholder="座位">
						</uni-easyinput>
					</view>号
				</view>
				<view class="action-text">
					({{wolf_king}})号狼王自爆，进入黑夜
					<view class="input1">
						<uni-data-checkbox v-model="wolfKingActionRadio" :localdata="wolfKingAction">
						</uni-data-checkbox>
					</view>
				</view>
			</uni-card>
			<!-- 白天 神职行为 -->
			<uni-card v-show="selectedGameStageStatus === 'day'":is-shadow="true" class="action-card">
				<uni-title type="h2" title="神职行为" color="#2490ff"></uni-title>
				<view class="action-text">
					({{hunter}})号猎人开枪带走了<view class="input">
						<uni-easyinput trim="all" v-model="hunterShootNum" :styles="styles" placeholder="座位">
						</uni-easyinput>
					</view>号 <text class="rest-tips">——剩余次数:{{shootrest}}</text>
				</view>
				<view class="action-text">
				</view>
			</uni-card>
			<view class="dm-control">
				<view class="control-btn">
					<button class="mini-btn" type="primary" size="mini" @click="subAddAction">提交录入</button>
				</view>
				<view class="control-btn">
					<button class="mini-btn" type="warn" size="mini" @click="subClearAllAction">全部删除</button>
				</view>
			</view>
			<uni-section title="已录入信息(本回合)" type="line" class="playeraction-info">
				<uni-notice-bar text="该功能用于查看修改已提交信息, 可右划逐条或点击全部删除;如游戏进度已不在当前回合, 则删除与新增提交动作无效" />
				<uni-swipe-action class="action-card">
					<uni-swipe-action-item v-for="(item, index) in gameStageActionList" :right-options="item.options"
						:key="item.id" @change="swipeChange($event, index)" @click="swipeClick($event, index)">
						<view class="submit-box">
							<text class="submit-text">{{ item.content }}</text>
						</view>
					</uni-swipe-action-item>
				</uni-swipe-action>
			</uni-section>
		</uni-section>
		<view>
			<!-- 信息录入确认框 -->
			<uni-popup ref="addActionDialog" type="dialog">
				<uni-popup-dialog type="warn" :content="addActionConfirm" title="玩家行为录入确认" @confirm="addAction"
					@close="cancelAddAction">
				</uni-popup-dialog>
			</uni-popup>
		</view>
		<view>
			<!-- 网络请求结果确认框 -->
			<uni-popup ref="resultMessage" type="message">
				<uni-popup-message :type="msgType" :message="messageText" :duration="2000">
				</uni-popup-message>
			</uni-popup>
		</view>
	</view>
</template>

<script>
	import {addplayeraction,delplayeraction} from "@/common/api"
	import { GAME_ACTION } from  "@/common/utils"
	import {mapState,mapMutations,mapGetters,mapActions} from "@/store/helper/dm"
	export default {
		name: 'manage-gameinfo',
		options: { styleIsolation: 'shared' },
		data() {
			return {
				styles: {
					color: '#2979FF',
					borderColor: '#2979FF'
				},
				gameStageList: ["首夜", "警长竞选", "第一日", "第一夜", "第二日", "第二夜", "第三日", "第三夜", "第四日", "第四夜", "第五日", "第五夜", "第六日",
					"第六夜", "第七日",
					"第七夜", "第八日", "第八夜", "第九日", "第九夜", "第十夜", "警长竞选2"
				],
				selectedGameStage: "",
				selectedGameStageStatus: "",
				//录入行为展示控制
				//夜晚 狼人行为
				inputWerewolfAttackNum: "",
				inputWolfBeautyCharmNum: "",
				//夜晚 神职行为 
				inputSeerCheckNum: "",
				witchRescue: [{
					text: '是',
					value: 0
				}, {
					text: '否',
					value: 1
				}],
				witchRescueRadio: 1,
				witchPoisonNum: "",
				guarderGuardNum: "",
				//白天 警长竞选行为
				campaignPoliceNums: [],
				// 白天 警长退水行为
				exitCampaignPoliceNums: [6, 7],
				//白天 警长行为
				policeMoveNum: "",
				policeCallExileNum:"",
				//白天 狼人自爆行为
				werewolfBoomNum: "",
				whiteWolfKingShootNum: "",
				wolfKingActionRadio: 1,
				wolfKingAction: [{
					text: '是',
					value: 0
				}, {
					text: '否',
					value: 1
				}],
				// <!--白天 神职行为 -->
				hunterShootNum: "",
				// 录入信息列表
				addActionConfirm: "",
				addActionList: [],
				addActionQuery: [],
				werewolf: [],
				wolf_beauty: 0,
				wolf_king: 0,
				white_wolf_king: 0,
				witch: 0,
				hunter: 0,
				cupid:0,
				seer:0,
				guarder:0,
				police: 0,
				poisonrest: 0,
				rescuerest: 0,
				shootrest: 0,
				charrest: 0,
				stageActionList:[],
				msgType: '',
				messageText: '',
			};
		},
		computed: {
			...mapState([
				"user",
				"currentGameID",
				"currentGameDTO",
				"gameStatus",
				"gameStageCode",
				"gameResult",
				"gameSeatList",
				"policeNum"
			]),
		},
		watch: {
			selectedGameStage:{
				handler(newval){
					if (newval.includes("竞选")) {
						this.selectedGameStageStatus = "campaign"
					} else if (newval.includes("日")) {
						this.selectedGameStageStatus = "day"
					} else if (newval.includes("夜")) {
						this.selectedGameStageStatus = "night"
					}
				},
				immediate: true,
			},
		},
		onLoad(option) {
			//接收页面参数
			this.selectedGameStage = option.managestage
			this.stageActionList=uni.getStorageSync('GAME_ACTION') || []
			this.police=this.policeNum
			this.queryNumber()
		},
		methods: {
			messageToggle(type, text) {
				this.msgType = type
				this.messageText = text
				this.$refs.resultMessage.open()
			},
			queryNumber(){
				//处理特殊身份展示，查找角色对应的号码
				let filterParm =this.currentGameDTO.setting.gameRoleSettings.filter(n => {return n.count === 1})
				for (let i = 0; i < filterParm.length; i++) {
					this[filterParm[i].code] = this.currentGameDTO.players.filter(n => { return n.gameRole.code === filterParm[i].code})[0].seatNumber
				}
				//从狼人阵营里加载狼人列表（包含狼王等特殊身份）且为存活状态
				let werewolfList = this.currentGameDTO.players.filter(n => { return n.gameRole.camp === "werewolf" && n.alive===true})
				for (let i = 0; i < werewolfList.length; i++) {
					this.werewolf.push(werewolfList[i].seatNumber)
				}
				this.werewolf.sort(function(a, b){return a - b})
				//检查狼人猎杀参数
				console.log(this.werewolf.reduce(function(total,num){console.log(total,num); return total + Math.pow(2,num)},0))				
			},
			subAddAction() {
				//确认框信息展示 + 请求参数拼接
				this.addActionConfirm = "本回合待提交信息如下，请确认后提交\n"
				this.addActionList.splice(0)
				if (this.selectedGameStageStatus === "campaign") {
					let tmp1 = []
					let tmp2 = []
					for (let i = 0; i < this.campaignPoliceNums.length; i++) {
						tmp1.push(this.campaignPoliceNums[i])
						this.addActionQuery.push({
							"actionSeatNumber":parseInt(this.campaignPoliceNums[i]),
							"type": "campaign_police"
						})
						this.addActionList.push(this.campaignPoliceNums[i] + "号上警")
					}
					for (let j = 0; j < this.exitCampaignPoliceNums.length; j++) {
						tmp2.push(this.exitCampaignPoliceNums[j])
						this.addActionQuery.push({
							"actionSeatNumber": parseInt(this.exitCampaignPoliceNums[j]),
							"type": "exit_campaign_police"
						})
						this.addActionList.push(this.exitCampaignPoliceNums[j] + "号退水")
					}	
					this.addActionConfirm = this.addActionConfirm + this.addActionList
					tmp1.splice(0)
					tmp1.splice(0)
				} else if (this.selectedGameStageStatus === "night") {
					//狼人行为
					if (this.inputWerewolfAttackNum.length>0) {
						this.addActionList.push("狼人袭击了" + this.inputWerewolfAttackNum)
						//给后端的特殊处理 actionSeatNumber为∑2^n(n=场上狼人阵营号码牌累加)
						console.log(this.werewolf)
						this.addActionQuery.push({
								"actionSeatNumber": this.werewolf.reduce(function(total,num){return total + Math.pow(2,num)},0),
								"targetSeatNumber": parseInt(this.inputWerewolfAttackNum),
								"type": "werewolf_attack"
						})
					}
					if (this.inputWolfBeautyCharmNum !== "") {
						this.addActionList.push("狼美人魅惑了" + this.inputWolfBeautyCharmNum)
						this.addActionQuery.push({
							"actionSeatNumber": this.wolf_beauty,
							"targetSeatNumber": parseInt(this.inputWolfBeautyCharmNum),
							"type": "wolf_beauty_charm"
						})
						this.charrest
					}
					//神职行为
					if (this.inputSeerCheckNum !== "") {
						this.addActionList.push("预言家查验了" + this.inputSeerCheckNum)
						this.addActionQuery.push({
							"actionSeatNumber": this.seer,
							"targetSeatNumber": parseInt(this.inputSeerCheckNum),
							"type": "seer_check"
						})
					}
					if (this.witchRescueRadio === 0) {
						this.addActionList.push("女巫使用解药解救了" + this.inputWerewolfAttackNum)
						this.addActionQuery.push({
							"actionSeatNumber": this.witch,
							"targetSeatNumber": parseInt(this.inputWerewolfAttackNum),
							"type": "witch_rescue"
						})
						this.rescuerest= 0
					}
					if (this.witchPoisonNum !== "") {
						this.addActionList.push("女巫毒杀了" + this.witchPoisonNum)
						this.addActionQuery.push({
							"actionSeatNumber": this.witch,
							"targetSeatNumber": parseInt(this.witchPoisonNum),
							"type": "witch_poison"
						})
						this.poisonrest = 0
					}
					if (this.guarderGuardNum !== "") {
						this.addActionList.push("守卫守护了" + this.guarderGuardNum)
						this.addActionQuery.push({
							"actionSeatNumber": this.guarder,
							"targetSeatNumber": parseInt(this.guarderGuardNum),
							"type": "guarder_guard"
						})
					}
					this.addActionConfirm = this.addActionConfirm + this.addActionList
				} else if (this.selectedGameStageStatus === "day") {
					//警长行为
					if (this.policeMoveNum !== "") {
						this.addActionList.push("警长将警徽移交给了" + this.policeMoveNum + "号")
						this.addActionQuery.push({
							"actionSeatNumber": this.police,
							"targetSeatNumber": parseInt(this.policeMoveNum),
							"type": "police_move"
						})
					}
					if (this.policeCallExileNum !== "") {
						this.addActionList.push("警长归票" + this.policeCallExileNum + "号")
						this.addActionQuery.push({
							"actionSeatNumber": this.police,
							"targetSeatNumber": parseInt(this.policeCallExileNum),
							"type": "police_call_exile"
						})
					}
					//狼人行为
					if (this.werewolfBoomNum !== "") {
						this.addActionList.push(this.werewolfBoomNum + "号狼人自爆")
						this.addActionQuery.push({
							"actionSeatNumber": this.werewolfBoomNum,
							"type": "werewolf_boom"
						})
					}
					if (this.whiteWolfKingShootNum !== "") {
						this.addActionList.push("白狼王自爆带走" + this.whiteWolfKingShootNum + "号")
						this.addActionQuery.push({
							"actionSeatNumber": this.white_wolf_king,
							"targetSeatNumber": parseInt(this.whiteWolfKingShootNum),
							"type": "werewolf_boom"
						})
					}
					if (this.wolfKingActionRadio === 0) {
						this.addActionList.push("狼王自爆进入黑夜")
						//暂无wolf_king_boom 行为
						// this.addActionQuery.push({
						// 	"actionSeatNumber": 0,
						// 	"targetSeatNumber": 0,
						// 	"type": "wolf_king_boom"
						// })
					}
					//神职行为
					if (this.hunterShootNum !== "") {
						this.addActionList.push("猎人开枪带走了" + this.hunterShootNum + "号")
						this.addActionQuery.push({
							"actionSeatNumber": this.hunter,
							"targetSeatNumber": parseInt(this.hunterShootNum),
							"type": "hunter_shoot"
						})
						this.shootrest=0
					}
					this.addActionConfirm = this.addActionConfirm + this.addActionList
				}
				this.$refs.addActionDialog.open()
			},
			async addAction() {
				const {code,data,message} = await addplayeraction({
					"actions":this.addActionQuery,
					"code":this.currentGameID
				})
				if (data.code === "200") {
					//添加已录入信息列表
					for (let i = 0; i < this.addActionList.length; i++) {
						this.stageActionList.push({
							options: [
								{
								text: this.addActionQuery[i],
								style: {
									backgroundColor: 'rgb(254,156,1)'
								}
								},{
								text: '删除',
								style: {
									backgroundColor: '#F56C6C'
								}
							}],
							id: i,
							content: this.addActionList[i]
						})
					}
					this.messageToggle('success', '录入玩家信息成功') 
					//本地存储所有录入行为
					uni.setStorageSync('GAME_ACTION',this.stageActionList)			
					this.setGameStageActionList(this.stageActionList)
				} else{
					this.messageToggle('error', data.message) 
					console.log(message)
				}
				this.addActionQuery.splice(0)
			},
			async swipeClick(e, index) {
				let {content,options} = e;
				if (content.text === '删除') {
					uni.showModal({
						title: '提示',
						content: '是否删除',
						success: res => {
							if (res.confirm) {	
							} else if (res.cancel) {
								console.log('用户点击取消');
								return 
							}
						}
					});
					const {code,data,message} = await delplayeraction({
					"actions":options[0].text,
					"code":this.currentGameID
					})
					if(data.code==='200'){
						this.messageToggle('success', '删除玩家信息成功')
						this.swipeList.splice(index, 1);
					}
					else{
						this.messageToggle('error', data.message)		
					}
				}
			},
			subClearAllAction(){
				this.messageToggle('warn', '该功能维护中，暂不可用')
			},
			cancelAddAction() {
				//取消提交 清空参数列表
				this.addActionQuery.splice(0)
			},
			...mapMutations([
				"setPoliceNum",
				"setGameStageActionList"
			]),
		},
	};
</script>

<style scoped lang="scss">
	.playeraction-info {
		height: 1268.11rpx;
	}
	
	.toggle-text{
		margin-left: 27.17rpx;
		width: 271.73rpx;
	}

	.action-text {
		display: flex;
		justify-content: flex-start;
		align-items: center;
		flex-wrap: wrap;
		flex: 1
	}

	.input {
		display: flex;
		align-items: center;
		height: 54.34rpx;
		width: 90.57rpx;
		margin: 0 5px;
		flex-wrap: wrap;
	}

	.input1 {
		display: flex;
		justify-content: flex-start;
		align-items: center;
		height: 65.21rpx;
		margin-left: 10px;
		flex-wrap: warp;
	}

	.dm-control {
		display: flex;
		justify-content: center;
		align-items: center;
		flex: 1;
	}

	.control-btn {
		margin: 20px 10px;
	}

	.submit-box {
		flex: 1;
		/* #ifdef APP-NVUE */
		justify-content: center;
		/* #endif */
		height: 79.71rpx;
		line-height: 79.71rpx;
		padding: 0 15px;
		position: relative;
		background-color: #fff;
		border-bottom-color: #f5f5f5;
		border-bottom-width: 1px;
		border-bottom-style: solid;
	}

	.submit-text {
		font-size: 15px;
		color: #6a6a6a;
	}

	.rest-tips {
		color: $uni-color-primary;
		font-size: $uni-font-size-sm;
	}
	/* 修改单选框选项的外边距 适应页面宽度*/
	.uni-data-checklist .checklist-group .checklist-box{
		margin-right: 0px !important;
	}
</style>
